//
//  WPTradeListViewModel.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/12.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPTradeListViewModel.h"
#import "WPRightTransModel.h"
#import "WPRightTransTicketDetailModel.h"


@implementation WPTradeListViewModel
+ (void)getTradeListWithPageNumber:(NSInteger)page areaid:(NSInteger)areaid typeid:(NSInteger)type dateSortAsc:(NSInteger)dateAsc priceSortAsc:(NSInteger)priceSortAsc andWithSuccessBlock:(void (^)(NSArray *, WPRightTransTicketDetailModel *, NSInteger))succ andWithFailureBlock:(void (^)(NSString *))fail{
    NSMutableDictionary * params = [NSMutableDictionary dictionaryWithDictionary:@{@"pageNumber":@(page), @"pageSize":@(10), @"goodsid":@(77)}];
    if (areaid != -1) {
        params[@"areaid"] = @(areaid);
    }
    if (type != -1) {
        switch (type) {
            case 0:
                params[@"typeid"] = @"SINGLE";
                break;
            case 1:
                params[@"typeid"] = @"PAIR";
                break;
            case 2:
                params[@"typeid"] = @"COMBINATION";
                break;
   
            default:
                break;
        }
    }
    if (dateAsc != -1) {
        params[@"dateSort"] = dateAsc ? @"asc":@"desc";
    }
    if (priceSortAsc != -1) {
        params[@"priceSort"] = dateAsc ? @"asc":@"desc";
    }
    [NetWorkingManager postWithUrl:@"commoditytrade/goodsDetail.jhtml" requestParams:[NetWorkingManager paramsByAppendingUserInfo:params] SuccessHandler:^(id responseObject) {
        if (responseObject[@"resultType"] && [responseObject[@"resultType"] intValue]) {
            if ([responseObject[@"resultMsg"] isKindOfClass:[NSArray class]]) {
                NSMutableArray * resultArr = [NSMutableArray arrayWithCapacity:0];
                for (int i = 0; i < [responseObject[@"resultMsg"] count]; i++) {
                    WPRightTransModel * model = [WPRightTransModel new];
                    model.salePersonName = responseObject[@"resultMsg"][i][@"salePerson"][@"username"];
                    [model setValuesForKeysWithDictionary:responseObject[@"resultMsg"][i]];
                    [resultArr addObject:model];
                }
                WPRightTransTicketDetailModel * model = [WPRightTransTicketDetailModel new];
                if (responseObject[@"value"]) {
                    [model setValuesForKeysWithDictionary:responseObject[@"value"]];
                }
                succ(resultArr, model, responseObject[@"totalpages"] ? [responseObject[@"totalpages"] integerValue]:0);
            } else {
                succ(@[], [WPRightTransTicketDetailModel new], 0);
            }
        } else {
            fail(responseObject[@"resultMsg"] && [responseObject[@"resultMsg"] length]? responseObject[@"resultMsg"] : @"暂无数据");
        }
    } failureHandler:^(NSError *error) {
        fail(error.localizedDescription);
    }];
}
@end
